package EGS.impl;
import java.util.List;
import java.util.concurrent.ExecutorService;

import javax.swing.JTable;

import org.dom4j.Document;
import org.dom4j.Element;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.stationClient;

public class EGS_execute_service5506Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 5506 组装过站
	 */
	public EGS_execute_service5506Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String transactionID = Mroot.element("header").attribute("transactionID").getValue();
			String iStation, iSN, iLayer,iState;
			int iiLayer,iiState;

			iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if (msgClass.equals("5506")) {// add by Akite 2020-04-30 edit by Akite 2020-11-17
				iSN = Mroot.element("body").element("panelcomp").element("main").element("panel").attribute("pcbID")
						.getValue();
				iLayer = Mroot.element("body").element("panelcomp").element("main").element("panel")
						.attribute("productSide").getValue();
				iState = Mroot.element("body").element("panelcomp").element("main").element("panel").attribute("state")
						.getValue();
				iiLayer = Integer.parseInt(iLayer);
				iiState = Integer.parseInt(iState);

				boolean bState = false;
				if (iiState == 0) {
					bState = true;
				}
//				System.out.println(Mroot.asXML());
				Element eleBody = Mroot.element("body").createCopy();
				Mroot.element("body").remove(Mroot.element("body").element("panelcomp"));
				Element eleR = Mroot.element("body").addElement("resultcomp");
				Element eleM = eleR.addElement("main");
				Element eleS = eleR.addElement("sub");
				Element eleL = eleR.addElement("label");
				boolean isget=false;
				try {
					
					if (iiLayer != iClient.myLayer) {
						String str = "工单激活面别与产品上传面别不一致！";
						eleM.addAttribute("errorCode", "1");
						eleM.addAttribute("errorText", str);
						eleM.addAttribute("actionCode", transactionID);
						AKSLogs(55060, "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
						return MSG;
					}
					isget=true;
				}catch(Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(55060, "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
				}
				if(isget) {
					try {
						String iLable = eleBody.element("panelcomp").element("label").element("panel").attribute("pcbID")
								.getValue();
						Element eleLR = eleL.addElement("result");
						eleLR.addAttribute("barcode", iLable);
						try {
							iClient.uploadState_label(iSN, bState, iLable);
							String errorCode, errorText, actionCode;
							errorCode = "0";
							errorText = "Valid";
							actionCode = "0";
							eleLR.addAttribute("errorCode", errorCode);
							eleLR.addAttribute("errorText", errorText);
							eleLR.addAttribute("actionCode", actionCode);
						} catch (Exception e) {
							String str = errCodeManagement.getErrText(e.getMessage());
							eleLR.addAttribute("errorCode", "1");
							eleLR.addAttribute("errorText", str);
							eleLR.addAttribute("actionCode", transactionID);
						}
					} catch (Exception e3) {
						;
					}
					try {
						Element eleSub = eleBody.element("panelcomp").element("sub");
						List<?> sons = eleSub.elements();
						boolean assemblyNG = false;
						String NGsons = "";
						if (sons.size() > 0) {
							String[] subParts = new String[sons.size()];
							String[] aState = new String[sons.size()];
							for (int n = 0; n < sons.size(); n++) {
								Element eleSR = eleS.addElement("result");
								subParts[n] = ((Element) sons.get(n)).attribute("pcbID").getValue();
								aState[n] = ((Element) sons.get(n)).attribute("state").getValue();
								eleSR.addAttribute("barcode", subParts[n]);
								try {
									iClient.assembling(iSN, subParts[n], aState[n]);
									eleSR.addAttribute("errorCode", "0");
									eleSR.addAttribute("errorText", "Valid");
									eleSR.addAttribute("actionCode", "0");
								} catch (Exception e2) {
									JTable t = sql.select("select * from assembled where masterSN='" + iSN
											+ "' and partSN='" + subParts[n] + "' ");
									if (t == null || t.getRowCount() < 1) {
										assemblyNG = true;
										if (NGsons.length() > 0) {
											NGsons = NGsons + ",";
										}
										NGsons = NGsons + subParts[n];
										String str = errCodeManagement.getErrText(e2.getMessage());
										eleSR.addAttribute("errorCode", "1");
										eleSR.addAttribute("errorText", str);
										eleSR.addAttribute("actionCode", aState[n]);
									} else {
										eleSR.addAttribute("errorCode", "0");
										eleSR.addAttribute("errorText", "Valid");
										eleSR.addAttribute("actionCode", "0");
									}
	
								}
							}
							String iiSN = iSN;
							boolean isFail = assemblyNG;
							new Thread(new Runnable() {
								String gSN = iiSN;
								boolean gIsFail = isFail;
//								stationClient uSC=iClient.copyStation(null);
								stationClient uSC=iClient;
								@Override
								public void run() {
									try {
										try {
											uSC.assembling(gSN, gSN, "0");
										} catch (Exception e3) {
											String str = errCodeManagement.getErrText(e3.getMessage());
											System.out.println(str);
										}
										if (!gIsFail) {
											uSC.uploadState(gSN, true,"-1");
										}
									} catch (Exception e2) {
										String str = errCodeManagement.getErrText(e2.getMessage());
										System.out.println(str);
									}
//									try {
//										uSC.close();
//									} catch (Exception e) {
//										;
//									}
								}
							}).start();
							String iErrCode = "0";
							String iErrText = "Valid";
							int iType = 55061;
							if (assemblyNG) {
								iErrCode = "1";
								iErrText = NGsons + "_装配失败，已有装配历史未拆分。";
								iType = 55060;
							}
							eleM.addAttribute("errorCode", iErrCode);
							eleM.addAttribute("errorText", iErrText);
							eleM.addAttribute("actionCode", "0");
							AKSLogs(iType, "" + iSN + "_" + iStation + "_" + iLayer);
							return MSG;
						}
					} catch (Exception e3) {
						;
					}
					eleM.addAttribute("errorCode", "0");
					eleM.addAttribute("errorText", "Valid");
					eleM.addAttribute("actionCode", "0");
					AKSLogs(55061, "" + iSN + "_" + iStation + "_" + iLayer);
				}

			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

}
